package com.atguigu.gmall.item.controller;


import lombok.SneakyThrows;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class  LockTestController {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate redisTemplate;


    @SneakyThrows
    @GetMapping("/write")
    public String readWrite(){

        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");

        RLock lock = readWriteLock.writeLock();

        lock.lock();

        String s = UUID.randomUUID().toString();
        Thread.sleep(10000);
        redisTemplate.opsForValue().set("mymsg", s);
        lock.unlock();


        return s;
    }
    @GetMapping("/read")
    public String read(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");

        RLock lock = readWriteLock.readLock();

        lock.lock();
        String mymsg = redisTemplate.opsForValue().get("mymsg");
        lock.unlock();

        return mymsg;

    }

    @GetMapping("/inittcc")
    public String tcc(int size){

        RSemaphore whsggdxtcc = redissonClient.getSemaphore("whsggdxtcc");
        whsggdxtcc.addPermits(size);
        return "whsggdxtcc 停车场有 "+size + "  个车位";
    }

    @GetMapping("/stopcar")
    public String stop() throws InterruptedException {
        RSemaphore whsggdxtcc = redissonClient.getSemaphore("whsggdxtcc");

        whsggdxtcc.acquire(1);  //从信号量里面拿一个




        return "停车成功...";
    }

    @GetMapping("/gocar")   //信号量
    public String start(){
        RSemaphore whsggdxtcc = redissonClient.getSemaphore("whsggdxtcc");


        whsggdxtcc.release(1);   //给信号量加值
        return "车开走了...";
    }


    //CountDownLatch 闭锁
    @GetMapping("/shenlong")
    public String shenlong() throws InterruptedException {
        RCountDownLatch zhsl = redissonClient.getCountDownLatch("zhsl");
        zhsl.trySetCount(7);  //需要七龙珠



        zhsl.await(); //等待龙珠集齐

        return "很大的神龙.....";
    }

    @GetMapping("/longzhu")
    public String longzhu(){
        RCountDownLatch zhsl = redissonClient.getCountDownLatch("zhsl");
        zhsl.countDown();

        return "收集了一颗龙珠";
    }

//
//    @GetMapping("/lock/hello")
//    public String hello(){
//
//        //执行业务逻辑
//
//
//
////    }

    /**
     * 以前用的 List，Map都在 内存中存的数据
     *
     * 分布式集合。
     *    1）、接下创建的集合里面存的数据都是在redis里面所有人都能用
     */
    @GetMapping("/pua")
    public String distributeCollection(){
        RMap<String, String> temp = redissonClient.getMap("temp");

//        Map  map = new HashMap();
//        map.put()
//        map.put()

        String string = UUID.randomUUID().toString();
        temp.put("hello",string);
        return string;
    }


    @GetMapping("/gea")
    public String getDistributeCollection(){
        RMap<String, String> temp = redissonClient.getMap("temp");
        String hello = temp.get("hello");
        return hello;
    }



}
